package gan;

import java.io.File;

import jmp123.gui.PlayList;
import jmp123.gui.PlayListThread;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class PlayerContainer {
	private Composite mainComp, subComp1, subComp2;
	private Button playButton, stopButton;
	private PlayListThread playListThread;
	private int status = 0;
	private CutSaveAudio audio;
	private PlayList list;
	private Label label;
	private Text outputDisplay;

	private Text inputLabel, outputDirText, preNameText;

	public void setOutputFilePrefix(String preName) {
		preNameText.setText(preName);
	}

	public void setOutputDir(String outputDir) {
		outputDirText.setText(outputDir);
	}

	public String getOutputDir(String outputDir) {
		return outputDirText.getText();
	}

	public Composite getComposite() {
		return mainComp;
	}

	public void setInput(File inputFile) {
		inputLabel.setText(inputFile.getAbsolutePath());
	}

	public PlayerContainer(Composite parent) {
		intGui(parent);
	}

	public void run() {
		list = new PlayList();
		audio = new CutSaveAudio(41000, label, this);
		addActions();
	}

	public void doStop() {
		if (playListThread != null && playListThread.isAlive()) {

			playListThread.interrupt();
			playListThread = null;
			status = 1;
		}
		stopButton.setEnabled(false);
		playButton.setText(" Play ");
		setCompChildren(subComp2, true);
	}

	private void setCompChildren(Composite comp, boolean b) {
		for (Control c : comp.getChildren()) {
			c.setEnabled(b);
		}
	}

	public String getPreName() {
		return preNameText.getText();
	}

	public String getOutputDir() {
		return outputDirText.getText();
	}

	public Text getOuptDisplay() {
		return outputDisplay;
	}

	private void intGui(Composite parent) {
		mainComp = new Composite(parent, SWT.NONE);
		mainComp.setLayout(new GridLayout(2, false));
		subComp1 = new Composite(mainComp, SWT.BORDER);
		{
			GridData gd = new GridData();
			gd.widthHint = 200;
			subComp1.setLayoutData(gd);
		}

		subComp1.setLayout(new GridLayout(3, false));
		playButton = new Button(subComp1, SWT.NONE);
		playButton.setText(" Play ");
		stopButton = new Button(subComp1, SWT.NONE);
		stopButton.setText(" Stop ");
		stopButton.setEnabled(false);
		label = new Label(subComp1, SWT.NONE);
		{
			GridData gd = new GridData();
			gd.widthHint = 80;
			gd.horizontalIndent = 2;
			label.setLayoutData(gd);
		}
		outputDisplay = new Text(mainComp, SWT.BORDER | SWT.MULTI | SWT.WRAP);
		{
			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.verticalSpan = 2;
			gd.widthHint = 100;
			outputDisplay.setLayoutData(gd);
			outputDisplay.setEditable(false);
		}

		subComp2 = new Composite(mainComp, SWT.BORDER);
		subComp2.setLayout(new GridLayout(3, false));
		{
			GridData gd = new GridData();
			gd.widthHint = 200;
			subComp2.setLayoutData(gd);
		}
		new Label(subComp2, SWT.NONE).setText("Prefix:");

		preNameText = new Text(subComp2, SWT.BORDER);
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			gd.horizontalSpan = 2;
			preNameText.setLayoutData(gd);
		}
		new Label(subComp2, SWT.NONE).setText("Input:");
		inputLabel = new Text(subComp2, SWT.BORDER);
		inputLabel.setEditable(false);
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			inputLabel.setLayoutData(gd);
		}
		{
			Button b1 = new Button(subComp2, SWT.NONE);
			b1.setText(">");
			b1.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event e) {
					FileDialog dialog = new FileDialog(inputLabel.getShell(),
							SWT.OPEN);
					dialog.setFilterExtensions(new String[] { "*.mp3", "*.*" });

					String dir = inputLabel.getText();
					if (!dir.equals("")) {
						File file = new File(dir);
						if (file != null) {
							String parent = file.getParent();
							if (new File(parent).isDirectory()) {
								dialog.setFilterPath(parent);
							}
						}
					}

					String filePath = dialog.open();
					if (filePath != null) {
						inputLabel.setText(filePath);
					}
				}
			});
		}

		new Label(subComp2, SWT.NONE).setText("Output:");
		outputDirText = new Text(subComp2, SWT.BORDER);
		outputDirText.setEditable(false);
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			outputDirText.setLayoutData(gd);
		}
		{
			Button b1 = new Button(subComp2, SWT.NONE);
			b1.setText(">");
			b1.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event e) {
					DirectoryDialog dialog = new DirectoryDialog(outputDirText
							.getShell(), SWT.OPEN);
					String dir = outputDirText.getText();
					if (!dir.equals("")) {
						File file = new File(dir);
						if (file != null) {
							String parent = file.getParent();
							if (new File(parent).isDirectory()) {
								dialog.setFilterPath(parent);
							}
						}
					}

					String filePath = dialog.open();
					if (filePath != null) {
						outputDirText.setText(filePath);
					}
				}
			});
		}

		mainComp.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent arg0) {
				if (playListThread != null && playListThread.isAlive()) {
					System.exit(0);
				}
			}
		});
	}

	private void addActions() {
		playButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				if (playListThread == null) {
					audio.update();
					playListThread = new PlayListThread(list, audio);
					list.append("", inputLabel.getText());

				}
				if (!playListThread.isAlive()) {
					playListThread.start();
					status = 1;
					playButton.setText(" Pause ");
					setCompChildren(subComp2, false);
					stopButton.setEnabled(true);
				} else if (status == 1) {
					status = 2;
					playListThread.pause();
					playButton.setText(" Play ");
					// stopButton.setEnabled(false);
					// setCompChildren(subComp2, true);
				} else if (status == 2) {
					playListThread.pause();
					status = 1;
					stopButton.setEnabled(true);
					playButton.setText(" Pause ");
					setCompChildren(subComp2, false);
				}

			}
		});

		stopButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				doStop();
			}
		});

	}

	public static void main(String args[]) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());

		PlayerContainer playerContainer = new PlayerContainer(shell);
		File file = new File("r:/test.mp3");
		playerContainer.setInput(file);
		playerContainer.setOutputFilePrefix("p");
		playerContainer.setOutputDir("e:/ttt/");
		playerContainer.run();

		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}

	public File getInput() {
		File file = new File(inputLabel.getText());
		return file;
	}
}
